const context = require.context("./modules", false, /\.js$/);
const modulesAll = [];
const modules = context.keys().reduce((total, path) => {
  const filename = path.replace(/\.\/|\.js/g, "");
  const content = context(path).default;
  const handleRouter = ({ children = [], ...value }) => {
    const { name, redirect, meta = {}, path, params = [], ...val } = value;
    const { parent, relation, prefix = filename } = meta;
    const pre = prefix ? `${prefix}_` : "";
    parent && (meta.parent = `/${pre}${parent}`);
    relation &&
      (meta.relation = Object.entries(relation).reduce((t, [k, v]) => {
        t[k] = v.parent ? { ...v, parent: `/${pre}${v.parent}` } : v;
        return t;
      }, {}));
    const route = redirect
      ? { ...value, redirect: `${pre}${redirect}` }
      : children.length
      ? { ...value, children: children.map(handleRouter) }
      : {
          ...val,
          name: `${pre}${name}`,
          path:
            path || params.reduce((t, i) => (t += `/:${i}`), `/${pre}${name}`),
          meta: { ...meta, prefix },
        };
    !modulesAll[prefix] && (modulesAll[prefix] = []);
    modulesAll[prefix].push(route);
    return route;
  };
  total[filename] = Object.entries(content).map(([key, value]) => {
    console.log(key);
    return handleRouter(value);
  });
  return total;
}, {});
console.log("modulesAll:", modulesAll);
const routes = [...modules.notcheck];
console.log("routes:", routes);
export default routes;
